import 'package:flutter/material.dart';

import 'package:junyeyishu/utils/utils.dart';
import 'package:junyeyishu/widgets/QGWidget.dart';

class QGShippingAddressDialog extends StatefulWidget {
  QGShippingAddressDialog({super.key});

  @override
  State<QGShippingAddressDialog> createState() =>
      _QGShippingAddressDialogState();
}

class _QGShippingAddressDialogState extends State<QGShippingAddressDialog> {
  final addressController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Stack(children: [
      Container(
        height: 300.rpx,
        padding: EdgeInsets.fromLTRB(20.rpx, 25.rpx, 20.rpx, 0.rpx),
        child: Column(
          children: [
            buildTitle(),
            SizedBox(height: 24.rpx),
            buildInput(),
            SizedBox(height: 30.rpx),
            buildButton()
          ],
        ),
      ),
      Positioned(
          right: 15.rpx,
          top: 15.rpx,
          width: 15.rpx,
          height: 15.rpx,
          child: GestureDetector(
            onTap: () {
              Navigator.of(context).pop();
            },
            child: QGImage(asset: 'mine_nickname_close'),
          ))
    ]);
  }

  buildTitle() {
    return Text(
      '修改收货地址',
      style: TextStyle(
          fontWeight: FontWeight.bold,
          fontSize: 16.rpx,
          color: Color(0xFFffffff)),
    );
  }

  buildInput() {
    return Row(
      children: [
        Expanded(
            child: Container(
          padding: EdgeInsets.all(15.rpx),
          height: 100.rpx,
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(15.rpx),
            color: Color(0xFF0C1012),
          ),
          child: TextField(
            controller: addressController,
            keyboardType: TextInputType.text,
            cursorColor: Color(0xFFffffff),
            maxLines: 4,
            style: TextStyle(
                fontWeight: FontWeight.normal,
                fontSize: 13.rpx,
                color: Color(0xFFffffff)),
            decoration: InputDecoration(
              hintText: '请输入收货地址',
              hintStyle: TextStyle(color: Color(0xFF989EA4)),
              border: const OutlineInputBorder(borderSide: BorderSide.none),
              contentPadding: EdgeInsets.zero,
              enabledBorder: InputBorder.none,
              focusedBorder: InputBorder.none,
            ),
          ),
        ))
      ],
    );
  }

  buildButton() {
    return Row(
      children: [
        Expanded(
            child: Container(
                height: 44.rpx,
                decoration: BoxDecoration(
                  gradient: const LinearGradient(
                    begin: Alignment.centerLeft,
                    end: Alignment.centerRight,
                    colors: [Color(0xFF4AE3FF), Color(0xFF44FFF0)],
                  ),
                  borderRadius: BorderRadius.circular(19.rpx),
                ),
                child: TextButton(
                  onPressed: () {
                    toModifyAddress();
                  },
                  child: Text(
                    '确定',
                    style: TextStyle(
                        fontSize: 14.rpx,
                        fontWeight: FontWeight.bold,
                        color: Color(0xFF111111)),
                  ),
                )))
      ],
    );
  }

  toModifyAddress() {
    var address = addressController.text;
    if (address.isEmpty) {
      toast('请输入收货地址');
      return;
    }
    unfocus(context);

    var param = {};
    param['address'] = address;
Map<String, dynamic> stringMap = param.cast<String, dynamic>();
    Network.post(url_u_modifyShippingAddress, data: stringMap).then((res) {
      toast(res.msg);
      if (res.success) {
        Future.delayed(Duration(seconds: 1), () {
          Navigator.of(context).pop(res.data["shipping_address"]);
        });
      }
    });
  }
}
